const register_a_li = document.querySelector('.topul a:nth-child(3) li')
const xhr = new XMLHttpRequest
const username = document.querySelector('.zz li')
username.onclick = function(){
    if(username.innerHTML != ' 请登录'){
        location.href = 'http://localhost:65535/personalCenter.html'
    }else{
        location.href = 'http://localhost:65535/login.html'
    }
}
register_a_li.onclick = function(){
    location.href = 'http://localhost:65535/register.html'
}
xhr.open('get',`http://localhost:65535/getUser?username=${document.cookie.substring(9)}`,true)
xhr.onload = function(){
    if(xhr.status >= 200 && xhr.status < 300){
        if(JSON.parse(xhr.response).code == 0){
            const response_parse = JSON.parse(xhr.response).data
            const tellogin_a_li = document.querySelector('.topul a:nth-child(4) li')
            username.innerHTML = response_parse.username
            register_a_li.innerHTML = '退出'
            tellogin_a_li.innerHTML = '消息中心'

            register_a_li.onclick = function(){
                const d = new Date
                d.setDate(d.getDate() - 1)
                document.cookie = `username=${username.innerHTML};path=/;expires=${d}`
                location.reload()
            }
        }else{
            console.log(xhr.response)
            
            const d = new Date
            d.setDate(d.getDate() - 1)
            document.cookie = `username=${username.innerHTML};path=/;expires=${d}`
        }
    }
}
xhr.send()

const search_input = document.getElementById('text')
const search_button = document.getElementById('text1')
const search_result_list = document.getElementsByClassName('search_result_list')[0]
let t_out
let this_value_replace
search_input.oninput = function(){
    clearTimeout(t_out)
    t_out = setTimeout(()=>{
        this_value_replace = this.value.replace(new RegExp(' ','g'),'')
        if(this_value_replace != ''){
            const script = document.createElement('script')
            script.src = `http://localhost:65535/jsonpSearch?searchStr=${this_value_replace}`
            document.body.appendChild(script)
            window.jsonp_search_fn = function(dataArr){
                let keywords = []
                dataArr.forEach((val,index) => {
                    keywords.push(...val.keywords.split(','))
                });
                const keywords_set = new Set(keywords)
                const keywords_norepeat = [...keywords_set]
                let str = ''
                keywords_norepeat.forEach((val,index)=>{
                    if(index < 10 && val.includes(this_value_replace)){
                        str += `<li>${this_value_replace}<b style="font-weight:700;">${val.substr(this_value_replace.length)}</b></li>`
                    }
                })
                search_result_list.innerHTML = str
            }
        }else{
            search_result_list.innerHTML = ''
        }
    },1000)
}
document.addEventListener('click',function(){
    search_result_list.style.display = 'none'
})
search_input.onclick = function(e){
    search_result_list.style.display = 'block'
    e.stopPropagation()
}
search_result_list.onclick = function(e){
    location.href = `http://localhost:65535/Search_results.html?keywords=${e.target.innerText}`
}


const cart_num = document.querySelector('.box2 a p em')
let goods = localStorage.getItem("goods");
goods = goods ? JSON.parse(goods) : [];
cart_num.innerHTML = goods.reduce((prev,val)=> prev + val.num,0);